package wci.frontend.pascal.parsers;

import wci.frontend.*;
import wci.frontend.pascal.*;
import wci.intermediate.*;

import static wci.frontend.pascal.PascalTokenType.*;
import static wci.frontend.pascal.PascalErrorCode.*;
import static wci.intermediate.icodeimpl.ICodeNodeTypeImpl.*;
import static wci.intermediate.icodeimpl.ICodeKeyImpl.*;

/**
 * <h1>RepeatStatementParser</h1>
 *
 * <p>解析Pascal REPEAT语句</p>
 *
 * <p>Copyright (c) 2009 by Ronald Mak</p>
 * <p>For instructional purposes only.  No warranties.</p>
 */
public class RepeatStatementParser extends StatementParser
{
    public RepeatStatementParser(PascalParserTD parent)
    {
        super(parent);
    }

    public ICodeNode parse(Token token)
        throws Exception
    {
    	//吞噬掉语句开始的REPEAT
        token = nextToken();  

        ICodeNode loopNode = ICodeFactory.createICodeNode(LOOP);
        ICodeNode testNode = ICodeFactory.createICodeNode(TEST);

        //解析包含在REPEAT和UNTIL中间的语句列表
        StatementParser statementParser = new StatementParser(this);
        statementParser.parseList(token, loopNode, UNTIL, MISSING_UNTIL);
        token = currentToken();

        //最后的测试表达式
        ExpressionParser expressionParser = new ExpressionParser(this);
        testNode.addChild(expressionParser.parse(token));
        loopNode.addChild(testNode);//最后一个子节点

        return loopNode;
    }
}
